var defaultHeaderStyle = {
  fontColor: '888888',
  horizontalAlignment: 'left',
  fontFamily: '等线',
  bold: true
}

var defaultCellStyle = {
  fontColor: '000000',
  horizontalAlignment: 'left',
  fontFamily: '等线'
}

function getOutput (headers, tableData, isRangeMode, exchangeAxis) {
  return XlsxPopulate.fromBlankAsync().then(workbook => {
    var sheet = workbook.sheet(0)
    if (isRangeMode) {
      var rangeInfos = headers
      if (rangeInfos && rangeInfos.length) {
        for (let index = 0; index < rangeInfos.length; index++) {
          var rangeInfo = rangeInfos[index]
          var style = Object.assign({}, defaultCellStyle, rangeInfo.style)
          var range = sheet.range(
            rangeInfo.startRow,
            rangeInfo.startCol,
            rangeInfo.endRow,
            rangeInfo.endCol
          )
          range.merged(rangeInfo.merged)
          range.value(rangeInfo.value)
        }
      }
    } else {
      if (headers && headers.length) {
        for (let index = 0; index < headers.length; index++) {
          var header = headers[index]
          var name = header.name
          var style = Object.assign({}, defaultHeaderStyle, header.headerStyle)
          var width = header.width

          if (exchangeAxis) {
            sheet.cell(index + 1, 1).value(name)
            
          } else {
            sheet.column(index + 1).width(width || 8.5)
            sheet.cell(1, index + 1).value(name)
              
          }
        }
      }
  
      if (headers && headers.length && tableData && tableData.length) {
        for (let index = 0; index < tableData.length; index++) {
          for (let indexInHeaders = 0; indexInHeaders < headers.length; indexInHeaders++) {
            var header = headers[indexInHeaders]
            var width = header.width
            var prop = header.prop
            var style = Object.assign({}, defaultCellStyle, header.style)

            if (exchangeAxis) {
              sheet.column(index + 1).width(width || 8.5)
              sheet.cell(indexInHeaders + 1, index + 2).value(tableData[index][prop])
              
            } else {
              sheet.cell(index + 2, indexInHeaders + 1).value(tableData[index][prop])
                
            }
          }
        }
      }
    }

    return workbook.outputAsync({ type: null }) // type: 'base64'
    // 公式求和暂无
    // for (let indexInHeaders = 0; indexInHeaders < headers.length; indexInHeaders++) {
    //   var header = headers[indexInHeaders]
    //   var numberFormat = header.numberFormat

    //   var sc = sheet.cell(2, indexInHeaders + 1)
    //   var ec = sheet.cell(tableData.length + 1, indexInHeaders + 1)

    //   sheet.cell(tableData.length + 2, indexInHeaders + 1).value(0).formula('=SUM(' + sc.columnName()+'2' + ':' + ec.columnName() + (tableData.length + 1) + ')')
    //     .style('horizontalAlignment', 'right')
    //     .style("numberFormat", numberFormat || '0')
    // }
  })
}

onmessage = function ({data: {headers, tableData, isRangeMode, exchangeAxis}}) {
  getOutput(headers, tableData, isRangeMode, exchangeAxis).then((blob) => {
    postMessage(blob)
  })
}